﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Messages;
using Rhino.ServiceBus;
using Rhino.ServiceBus.Hosting;
using Rhino.ServiceBus.Msmq;
using Utils;

namespace Buyer
{
    public partial class Form1 : Form
    {

        private string buyerName;
        private string buyerItem;
        private string buyerSize;
        private Guid buyerGuid;
        private BuyerController buyerController;
        private IServiceBus bus;

        public static Form1 _Form1;

        public Form1()
        {
            InitializeComponent();

            _Form1 = this;

            PrepareQueues.Prepare("msmq://localhost/KSRRhinoSelfHost_Buyer", QueueType.Standard);
            PrepareQueues.Prepare("msmq://localhost/KSRRhinoSelfHost_Shop", QueueType.Standard);
            PrepareQueues.Prepare("msmq://localhost/KSRRhinoSelfHost_Seller", QueueType.Standard);

            var buyerHost = new DefaultHost();

            buyerGuid = Guid.NewGuid();

            buyerHost.Start<BuyerBootStrapper>();
            bus = buyerHost.Bus as IServiceBus;

            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");

            this.comboBox2.Items.Add("Small");
            this.comboBox2.Items.Add("Medium");
            this.comboBox2.Items.Add("Big");

            addToLog("Hello Buyer!");
            addToLog("Select your name and press Save button.");
        }

        public void addToLog(String message)
        {
            string log = richTextBox1.Text;
            log += "\n" + message;
            richTextBox1.Text = log;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!this.textBox1.Text.Equals(""))
            {
                buyerName = textBox1.Text;
                addToLog("Hello " + buyerName);
                addToLog("Select item.");
                this.comboBox1.Enabled = true;
            }
            else
            {
                addToLog("Insert your name.");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            buyerItem = comboBox1.SelectedItem.ToString();
            addToLog("Item selected: " + buyerItem);
            addToLog("Select size.");
            comboBox2.Enabled = true;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            buyerSize = comboBox2.SelectedItem.ToString();
            addToLog("Size selected: " + buyerSize);
            addToLog("Click Buy button");
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            buyerController = new BuyerController(bus, this) {
                BuyerGuid = buyerGuid,
                Item = buyerItem,
                Name = buyerName,
                Size = ItemSize.Medium
            };

            buyerController.BuyItemSync();
        }
    }
}
